<?php

namespace frontend\widgets\article;

use Yii;
use yii\base\Widget;
use common\models\ArticleModel;
use frontend\models\Article;
use yii\helpers\Url;
use yii\data\Pagination;

/**
 * 文章列表组件
 * @author admin
 *
 */
class ArticleWidget extends Widget
{
    /**
     * 显示文章标题
     * @var unknown
     */
    public $title = '';
    /**
     * 显示文章条数
     * @var unknown
     */
    public $limit = 6;
    /**
     * 是否显示更多
     * @var unknown
     */
    public $more = true;
    /**
     * 是否显示分页
     * @var unknown
     */
    public $page = true;
    
    public function run()
    {
        //获取当前页
        $curPage = Yii::$app->request->get('page', 1);
        //查询条件
        $cond = ['=', 'is_valid', ArticleModel::IS_VALID];
        //获取文章列表, $this->limit实际上是 $pageSize，显示多少条数据
        $res = Article::getList($cond, $curPage, $this->limit);
        //得到结果
        $result['title'] = $this->title?:"最新文章";
        $result['more'] = Url::to(['article/index']);
        $result['body'] = $res['data']?:[];
        
        //是否显示分页
        if ($this->page) {
            $pages = new Pagination(['totalCount' => $res['count'], 'pageSize' => $res['pageSize']]);
            $result['page'] = $pages;
            
        }
        return $this->render('index', ['data' => $result]);
    }
}